<template>
  <div id="userloginPage">
    <div class="title-container">
      <h2 class="title">用户登录</h2>
    </div>
    <a-form
      style="max-width: 480px; margin: 0 auto"
      :model="formState"
      name="basic"
      label-align="left"
      :label-col="{ span: 50 }"
      :wrapper-col="{ span: 60 }"
      autocomplete="off"
      @finish="handleSubmit"
    >
      <a-form-item
        label="账号"
        name="userAccount"
        :rules="[{ required: true, message: '请输入您的用户名！' }]"
      >
        <a-input
          v-model:value="formState.userAccount"
          placeholder="请输入账号！"
        />
      </a-form-item>

      <a-form-item
        label="密码"
        name="userPassword"
        :rules="[
          { required: true, message: '请输入密码!' },
          { min: 8, message: '密码不能小于8位' },
        ]"
      >
        <a-input-password
          v-model:value="formState.userPassword"
          placeholder="请输入您的密码！"
        />
      </a-form-item>

      <a-form-item name="remember" :wrapper-col="{ offset: 8, span: 16 }">
        <a-checkbox v-model:checked="formState.remember">记住密码 </a-checkbox>
      </a-form-item>

      <a-form-item :wrapper-col="{ offset: 4, span: 20 }">
        <a-button type="primary" html-type="submit" class="login-btn"
          >登录</a-button
        >
        <a-button type="default" class="register-btn" @click="goToRegister"
          >注册</a-button
        >
      </a-form-item>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive } from "vue";
import { userLogin } from "@/api/getCurrentUserRequest";
import { useLoginUserStore } from "@/store/useLoginUserStore";
import { message } from "ant-design-vue";
import { useRouter } from "vue-router";

interface FormState {
  userAccount: string;
  userPassword: string;
  remember: boolean;
}

const formState = reactive<FormState>({
  userAccount: "",
  userPassword: "",
  remember: false,
});

const router = useRouter();
const loginUserStore = useLoginUserStore();

/**
 * 用于表单提交执行登录
 * @param values
 */
const handleSubmit = async (values: any) => {
  const res = await userLogin(values);
  if (res.data.code === 0 && res.data.data) {
    await loginUserStore.fetchLoginUser();
    message.success("登录成功");
    router.push({
      path: "/",
      replace: true, // 用户在登录完之后不会回到登录页面，而是其他新的页面
    });
  }
  console.log("Success:", values);
};

const onFinishFailed = (errorInfo: any) => {
  console.log("Failed:", errorInfo);
};

/**
 * 跳转到注册页面
 */
const goToRegister = () => {
  router.push("/user/register");
};
</script>

<style scoped>
#userloginPage {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #e8e8e8;
  border-radius: 8px;
  background-color: #ffffff;
  padding-bottom: 20px;
}

.title {
  font-size: 36px;
  text-align: center;
  margin-bottom: 24px;
}

.title-container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 150px; /* 调整标题与表单的间距 */
}

.register-btn {
  margin-left: 180px; /* 增加注册按钮的左边距 */
}
</style>
